﻿
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Data.SqlClient;
using System.Collections.Generic;


namespace Patent.BLL.Reg
{
    /*
    /// <summary>
    /// 原产地类型
    /// </summary>
    public enum OriginPlaceType
    {
        /// <summary>
        /// 国家
        /// </summary>
        Countryin,//0
        /// <summary>
        /// 省份
        /// </summary>
        Countryout//1
        
    }
    /// <summary>
    /// RegSelect 的摘要说明
    /// </summary>
    public class RegSelect
    {
        
        #region 属性
        private int m_topicID;
        /// <summary>
        /// 专题号
        /// </summary>
        public int TopicID
        {
            get { return m_topicID; }
            set { m_topicID = value; }
        }
        private string m_topiRegame;
        /// <summary>
        /// 专题名称
        /// </summary>
        public string TopiRegame
        {
            get
            {
                if (string.IsNullOrEmpty(m_topiRegame))
                {
                    GetTopicInfo();
                }
                return m_topiRegame;
            }
        }
        private string m_topicTable;
        /// <summary>
        /// 信息表名
        /// </summary>
        public string TopicTable
        {
            get { return m_topicTable; }
        }
 
        private string  m_originPlaceTable;
        /// <summary>
        /// 原产区表
        /// </summary>
        public string  OriginPlaceTable
        {
            get { return m_originPlaceTable; }
            set { m_originPlaceTable = value; }
        }
	




        #endregion
        public RegSelect()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public RegSelect(int topicID)
        {
            m_topicID = topicID;
            GetTopicInfo();
        }
       

        /// <summary>
        /// 获取专题信息
        /// </summary>
        /// <returns></returns>
        public bool GetTopicInfo()
        {
            try
            {
                if (m_topicID < 0)
                {

                    throw new FormatException();
                }
                string sql = "select 专题名称,信息表名,原产区表 from ptcase_m where 专题编号=@topicID";
                Patent.DBUtility.SQLHelper helper = new Patent.DBUtility.SQLHelper();
                SqlParameter[] parms = new SqlParameter[1];
                parms[0] = helper.CreateInParam("@topicID", SqlDbType.Int, 0, m_topicID);
                SqlDataReader reader;
                helper.RunSQL(sql, parms, out reader);
                if (reader.HasRows)
                {
                    reader.Read();
                    m_topiRegame = reader.IsDBNull(0) ? "" : reader.GetString(0);
                    m_topicTable = reader.IsDBNull(1) ? "" : reader.GetString(1);
                    m_originPlaceTable = reader.IsDBNull(2) ? "" : reader.GetString(2);
                    //m_topicSource = reader.IsDBNull(3) ? "" : reader.GetString(3);
                }
                else
                {
                    throw new NullReferenceException();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;



        }
       
        /// <summary>
        /// 获取原产地名称列表
        /// </summary>
        /// <param name="type">原产地类型</param>
        /// <returns></returns>
        public List<string> GetOriginPlaceList(OriginPlaceType type)
        {
            if (string.IsNullOrEmpty(m_topicTable))
            {
                throw new FormatException("信息表名为空");
            }
            string sql=string.Empty;
            if (type == OriginPlaceType.Countryin)
            {
                sql = "select 国省代码 from "+m_originPlaceTable+" where 国省类型=0";
            }
            if (type == OriginPlaceType.Countryout)
            {
                sql = "select 国省代码 from " + m_originPlaceTable + " where 国省类型=1";
            }

            Patent.DBUtility.SQLHelper helper = new Patent.DBUtility.SQLHelper();
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = helper.CreateInParam("@InfoTable", SqlDbType.VarChar, 10, m_topicTable);
            DataSet ds = new DataSet();
            helper.RunSQL(sql, parms, ref ds);
            List<string> itemsList = new List<string>();
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    itemsList.Add(item[0].ToString());

                }
            }
            helper.Close();
            return itemsList;
        }

        /// <summary>
        /// 申请人专利统计
        /// </summary>
        /// <param name="places"></param>
        /// <returns></returns>
        public DataTable ApptPtStat(int topnum)
        {
            
            string sql = "";
            string sq = "";
            DataSet ds = new DataSet();
            Patent.DBUtility.SQLHelper helper = new Patent.DBUtility.SQLHelper();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("申请人", typeof(string)));
            dt.Columns.Add(new DataColumn("总数", typeof(int)));
            dt.Columns.Add(new DataColumn("百分比", typeof(double )));
            sql = "select count(*) from " + m_topicTable;
            helper.RunSQL(sql, ref ds);
            DataSet data = new DataSet();
            int total =Convert .ToInt32 (ds .Tables [0].Rows [0][0]);
            sq = "select top " + topnum +" 申请人,count(*)  from " + m_topicTable + 
               " group by [申请人] order by  count(*) desc";
            helper.RunSQL(sq, ref data );
            int num=data.Tables [0].Rows .Count;
            for(int i=0;i<num;i++)
            {
                DataRow dr = data.Tables[0].Rows[i];
                DataRow drfoo = dt.NewRow();
                drfoo["申请人"] = dr [0];
                drfoo["总数"] = dr[1];
                drfoo["百分比"] =Convert.ToDouble(dr[1])/(double)total;
               
                dt.Rows.Add(drfoo);
          
            }
  
            helper.Close();
            return dt;
        }

        public DataTable GetAllApplicantNames()
        {
            string sql = "select 申请人,count(*) as 数量 from " + m_topicTable + " group by 申请人 order by 数量 desc";
            DataSet ds = new DataSet();
            Patent.DBUtility.SQLHelper helper = new Patent.DBUtility.SQLHelper();
            helper.RunSQL(sql, ref ds);
            DataTable dt = new DataTable();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            helper.Close();

            return dt;


        }
        public DataTable GetApplicantNames(string keyword)
        {
            string sql = "select distinct top 10 申请人 from " + m_topicTable + " where 申请人 like '" + keyword + "%'";
            DataSet ds = new DataSet();
            Patent.DBUtility.SQLHelper helper = new Patent.DBUtility.SQLHelper();
            helper.RunSQL(sql, ref ds);
            DataTable dt = new DataTable();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            helper.Close();

            return dt;

        }
        
    }*/
}
